/** 下拉城市三级联动 **/
if( $("select#province_select").length >= 1 ) {
	$.post("/logistics.php/Public/area.html" , function(data){
		var area_data = data.info;
		$("select#province_select").each(function(){
			var _self = $(this).closest(".city_select_plug");
			var htmlStr = '<option value="">省</option>';
			var province_value = parseInt( _self.find("#province_select").attr("data-value") );
			var city_value = parseInt( _self.find("#city_select").attr("data-value") );
			var area_value = parseInt( _self.find("#area_select").attr("data-value") );
			$.each(area_data , function(k , v){
				htmlStr += '<option value="'+v.id+'" ';
				if( province_value > 0 && v.id == province_value ){
					htmlStr += ' selected ';
				}
				htmlStr += '>'+v.name+'</option>';
			});
			_self.find("#province_select").html(htmlStr);
			if( city_value > 0 ) {
				htmlStr = '<option value="">市</option>';
				$.each(area_data , function(k , v){
					if( v.id == province_value ){
						$.each(v.sub , function(k1 , v1){
							htmlStr += '<option data-pid="'+province_value+'" value="'+v1.id+'" ';
							if( city_value > 0 && v1.id == city_value ){
								htmlStr += ' selected ';
							}
							htmlStr += '>'+v1.name+'</option>';
						});
					}
				});
				_self.find("#city_select").html(htmlStr);
			}
			if( area_value > 0 ) {
				htmlStr = '<option value="">区</option>';
				$.each(area_data , function(k , v){
					if( v.id == province_value ){
						$.each(v.sub , function(k1 , v1){
							if( v1.id == city_value ) {
								$.each(v1.sub , function(k2 , v2){
									htmlStr += '<option value="'+v2.id+'" ';
									if( area_value > 0 && v2.id == area_value ){
										htmlStr += ' selected ';
									}
									htmlStr += '>'+v2.name+'</option>';
								});
							}
							
						});
					}
				});
				_self.find("#area_select").html(htmlStr);
			}
			_self.find("#province_select").change(function(){
				htmlStr = '<option value="">市</option>';
				var province_id = $(this).val();
				if( province_id == 0 ) {
					_self.find("#city_select").html('<option value="">市</option>');
				} else {
					$.each(area_data , function(k,v){
						if( v.id == province_id ) {
							$.each( v.sub , function(k1,v1){
								htmlStr += '<option data-pid="'+v1.parent_id+' "value="'+v1.id+'">'+v1.name+'</option>';
							});
						}
					});
					_self.find("#city_select").html(htmlStr);
				}
				_self.find("#area_select").html('<option value="">区</option>');
			});
			_self.find("#city_select").change(function(){	
				htmlStr = '<option value="">区</option>';
				var city_id = $(this).val();
				if( city_id == 0 ) {
					_self.find("#area_select").html('<option value="">区</option>');
				} else {
					var pid = parseInt( _self.find("#city_select option:selected").attr("data-pid") );
					$.each(area_data , function(k,v){
						if( v.id == pid ) {
							$.each( v.sub , function(k1,v1){
								if( v1.id == city_id ) {
									$.each( v1.sub , function(k2,v2){
										htmlStr += '<option value="'+v2.id+'">'+v2.name+'</option>';
									});
								}
							});
						}
					});
					_self.find("#area_select").html(htmlStr);
				}
			});
			
		});
	},'json');
}
// ====================================================================


/* ------------- 表单统一认证 -------------------------*/

$("form#mysubfrom").submit(function(){
	var rule_data = {
		dataType : {
			"*":/[\w\W]+/,
			"back":/[\w\W]+/,
			"*6-16":/^[\w\W]{6,16}$/,
			"n":/^\d+$/,
			"jine":/^\d+(\.)?\d{1,2}$/,
			"n6-16":/^\d{6,16}$/,
			"s":/^[\u4E00-\u9FA5\uf900-\ufa2d\w\.\s]+$/,
			"s6-18":/^[\u4E00-\u9FA5\uf900-\ufa2d\w\.\s]{6,18}$/,
			"p":/^[0-9]{6}$/,
			"m":/^13[0-9]{9}$|14[0-9]{9}|17[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$/,
			"e":/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
			"url":/^(\w+:\/\/)?\w+(\.\w+)+.*$/
		},
		tips : {
			"*":"不能为空！",
			"back":"不能为空！",
			"*6-16":"请填写6到16位任意字符！",
			"n":"请填写数字！",
			"n6-16":"请填写6到16位数字！",
			"s":"不能输入特殊字符！",
			"s6-18":"请填写6到18位字符！",
			"p":"请填写邮政编码！",
			"m":"请填写手机号码！",
			"e":"邮箱地址格式不对！",
			"url":"请填写网址！",
			"jine":"填写正确的金额"
		}
	}
	var form_index = true, _form_self = $(this);
	$("#mysubfrom").find("[datatype]").each(function(){
		var datatype = $(this).attr("datatype");
		var _self = $(this);
		var nullmsg = _self.attr("nullmsg"),errormsg = _self.attr("errormsg"),syssmg = rule_data.tips[datatype];
		if( !rule_data.dataType[datatype].test( _self.val() ) ){
			$.message({
				message:( _self.val() == '' ? ( nullmsg == '' || nullmsg == undefined ? syssmg : nullmsg ) : ( errormsg == '' || errormsg == undefined ? syssmg : errormsg ) ),
				type:'warning'
			});
			form_index = false;
			return false;
		}
		//绑定回调认证
		if( datatype == 'back' ) {
			var back_self = $("#" + _self.attr("data-elem"));
			if( back_self.length <= 0 ) {
				var error_msg = '自定义的ID元素不存在';
				form_index = false;
			}
			if( back_self.val() !=  _self.val() ) {
				var error_msg = errormsg == '' || errormsg == undefined ? '两次对比不一致' : errormsg;
				form_index = false;
			}
			if( form_index === false ) {
				$.message({
					message:error_msg,
					type:'warning'
				});
				return false;
			}
		}
	});
	if( form_index === true ) {
		$.ajax({
			dataType:'json',
			data : _form_self.serialize(),
			url : _form_self.attr("action"),
			type : 'post',
			error : function(){
				$.message({
					message: '请求返回错误',
					type:'error'
				});
			},
			success : function(data){
				if( data.status == 1 ) {
					$.message(data.info);
					setTimeout(function(){
						if( data.url == '' ) {
							location.reload();
						} else {
							location.href = data.url;
						}
					},1500);
				} else {
					$.message({
						message: data.info,
						type:'error'
					});
				}
			}
		});
	}
	return false;
});

//===========================================================================